'use strict';

const await = require('await-stream-ready/lib/await');
const baseController = require('../../utils/base')

class ProductController extends baseController {
    async index() {
        let page = this.ctx.query.page
        console.log(page,'---');
        if(!page){
            page = 1
        }
        var pageSize = 3

        let result = await this.app.mysql.query(`select * from product limit ${pageSize} offset ${(page-1)*pageSize}`)

        let resultAll = JSON.parse(JSON.stringify(result))
        for (let i = 0; i < result.length; i++) {
           let res =  await this.app.mysql.query('select * from product_cate where id= ?', [result[i].cid])
        //    console.log(res,res.length,"===");
            resultAll[i]['ProductCate'] = res.length > 0 ?res :[{}]
            // console.log(resultAll[i]);
        }
        // console.log(resultAll[0].ProductCate[0].title, '00--0');
        await this.ctx.render('admin/product/index', {
            list: resultAll
        })
    }
    async add() {
        // this.ctx.body = "添加"

        let result = await this.ctx.model.ProductCate.findAll();
        await this.ctx.render('admin/product/add', {
            cateList: result
        })
    }
    async doAdd() {

        let { cid, title, imgUrl, price, sort, status, isBest, isHot, content } = this.ctx.request.body
        var sql = 'insert into product (cid, title, price, img_url, content, sale_count, is_best, is_hot, sort)values (?,?,?,?,?,?,?,?,?);'
        var result = await this.app.mysql.query(sql, [cid, title, price, imgUrl, content, 1, isBest, isHot, sort])

        if (result.protocol41) {
            await this.success("添加菜品成功", `/${this.config.pathname}/product`);
        } else {
            await this.error("添加菜品失败", `/${this.config.pathname}/product`);
        }
    }
    async doUpload() {
        const stream = await this.ctx.getFileStream();
        // const result = await this.service.admin.upload.uploadStrom(stream) // 上传到本地
        const result = await this.service.admin.upload.uploadCos(stream) // 上传文件到腾讯云cos
        this.ctx.body = { link: "http://" + result.Location, ...result }
    }
    async del() {

        let { id } = this.ctx.query

        let result = await this.app.mysql.query('delete from product where id= ?', [id])
        if (result.protocol41) {
            await this.success("删除菜品成功", `/${this.config.pathname}/product`);
        } else {
            await this.error("删除菜品失败", `/${this.config.pathname}/product`);
        }
    }
    async edit() {

        let { id } = this.ctx.query

        let cateList = await this.ctx.model.ProductCate.findAll();
        let result = await this.app.mysql.query('select * from product where id = ?', [id])

        console.log(result, "===");
        await this.ctx.render('admin/product/edit', {
            product: result[0],
            cateList,
        })
    }

    async doEdit() {
        console.log(this.ctx.request.body, '===');
        let { cid, title, imgUrl, price, sort, status, isBest, isHot, content, id } = this.ctx.request.body
        var sql = 'update product set cid=?,title=?,price=?,img_url=?,content=?,sale_count=?,is_best=?,is_hot=?,sort=? where id=?'
        var result = await this.app.mysql.query(sql, [cid, title, price, imgUrl, content, 1, isBest, isHot, sort, id])
        if (result.protocol41) {
            await this.success("修改菜品成功", `/${this.config.pathname}/product`);
        } else {
            await this.error("修改菜品失败", `/${this.config.pathname}/product`);
        }
    }
}

module.exports = ProductController;